<%@page import="java.util.List,com.trw.trp.bean.ModelBean,com.trw.trp.bean.ModelDetail,com.trw.trp.bean.OrderBean,com.trw.trp.util.*"%>
<%@ include file="header.jsp"%>

<%
step=3;

OrderBean order = (OrderBean)session.getAttribute(TRPConstants.CURRENT_ORDER);
UserBean user = (UserBean)session.getAttribute(TRPConstants.CURRENT_USER);
List<ModelBean> modelList = order.getModelMap().get(1);
%>

<table align="center" cellpadding="0" cellspacing="0" height="100%"
	width="100%" border="0">
	<tr>
		<td valign="top">
		<div id="testPartsDiv" />
		</td>
	</tr>
</table>

<script type="text/javascript">

function goBack(){
	document.location='viewOrder.action?orderId='+<%=order.getOrderId() %>;
}

function validateFile(){
	var result='',grid;
	<%for(int i=0;i<modelList.size();i++){
		ModelBean model = modelList.get(i);
	%>
		grid = Ext.getCmp('fileGrid'+'<%=model.getClientUniqueId()%>');
		if(grid){
			if(grid.getStore().getCount()==0){
				return 'Please upload file for Test Parts Information!';
			}
		}
		grid = Ext.getCmp('fileGrid2'+'<%=model.getClientUniqueId()%>');
		if(grid){
			if(grid.getStore().getCount()==0){
				return 'Please upload file for BOM Information!';
			}
		}
	<%}%>
	return result;
}

function sendMail0(){
	Ext.getBody().mask('Sending mail...');
	Ext.Ajax.request({
		url: 'sendEmail.action',
		params: {
			to: '<%=order.getGeneralInfo().getTestUser().getEmail()%>',
			cc: '<%=order.getGeneralInfo().getVvUser().getEmail()%>',
			emailId: 8
		},
		success: function(response){
			var grid,formpnl;
			<%for(int i=0;i<modelList.size();i++){
				ModelBean model = modelList.get(i);
			%>
				grid = Ext.getCmp('fileGrid'+'<%=model.getClientUniqueId()%>');
				formpnl = Ext.getCmp('fileForm'+'<%=model.getClientUniqueId()%>');
				if(grid){
					grid.setDisabled(true);
					formpnl.setDisabled(true);
					grid.getView().refresh();
				}
				grid = Ext.getCmp('fileGrid2'+'<%=model.getClientUniqueId()%>');
				formpnl = Ext.getCmp('fileForm2'+'<%=model.getClientUniqueId()%>');
				if(grid){
					grid.setDisabled(true);
					formpnl.setDisabled(true);
					grid.getView().refresh();
				}
			<%}%>
			Ext.getBody().unmask();
			var msg = Ext.util.JSON.decode(response.responseText);
			Ext.MessageBox.alert(msg.state==0?'Info':'Warning', msg.message);
		},
		failure: function(response){
			Ext.getBody().unmask();
			var msg = Ext.util.JSON.decode(response.responseText);
			Ext.MessageBox.alert('Warning', msg.message);
		}
	});
}

function sendMail(){
	Ext.MessageBox.confirm('Confirm', 'All the information in this page will be frozen after your confirmation! Still want to continue?', function(btn){
		if(btn == 'yes'){
			var validateResult = validateFile();
			if(validateResult!=''){
				Ext.MessageBox.alert('Warning', validateResult);
				return;
			}
			Ext.getBody().mask('Saving data...');
			Ext.Ajax.request({
				url: 'updateTestPartInfo.action',
				success: function(response){
					Ext.getBody().unmask();
					var msg = Ext.util.JSON.decode(response.responseText);
					if(msg.state==0){
						sendMail0();
					}else{
						Ext.MessageBox.alert('Warning', msg.message);
					}
				},
				failure: function(response){
					Ext.getBody().unmask();
					var msg = Ext.util.JSON.decode(response.responseText);
					Ext.MessageBox.alert('Warning', msg.message);
				}
			});
		}
	});
}

var contractPnl;
Ext.onReady(function(){
	if(!contractPnl){
		contractPnl = Ext.create('Ext.panel.Panel',{
			forceFit: true,
			autoScroll:false,
			title:'<font size=3><b>Test Parts Information</b></font>',
			renderTo:'testPartsDiv',
			items:[{
				xtype:'label',
				colspan:2,
				cls:'x-form-item',
				html:'<br><b>Please upload Test Parts/BOM Information for each model *</b>(file size is limited to 5 MB)'
			}
			<%for(int i=0;i<modelList.size();i++){
				ModelBean model = modelList.get(i);
			%>
				,{
					xtype:'fieldset',
					title:'<%=model.getModelName()%>',
					layout: {
						type: 'table',
						columns: 3,
						tableAttrs: {
							border:0,
							width:'100%'
						}
					},
					autoScroll:false,
					style: {
						marginLeft: 'auto',
						marginRight: 'auto'
					},
					items:[{
						xtype:'displayfield',
						width:140,
						value:'Test Parts Information:'
					},{
						xtype:'form',
						fileUpload: true,
						id:'fileForm'+'<%=model.getClientUniqueId()%>',
						border:false,
						width:250,
						items:[{
							xtype: 'fileuploadfield',
							name: 'uploadFile',
							id:'uploadFile'+'<%=model.getClientUniqueId()%>',
							disabled:<%=order.getStatus()==3%>,
							buttonOnly: true,
							width:250,
							buttonText: 'Select File...',
							listeners: {
								'change': function(fb, v){
									if(v!=''&&v!=null){
										if(v.indexOf('\\')>-1){
											v = v.substr(v.lastIndexOf('\\')+1);
										}
										Ext.getCmp('fileGrid'+'<%=model.getClientUniqueId()%>').getEl().mask('uploading file...');
										Ext.getCmp('fileForm'+'<%=model.getClientUniqueId()%>').getForm().submit({
											url:'uploadFile.action',
											params:{
												orderNumber:'<%=order.getOrderNumber()%>',
												fileName: v,
												type:'matrixInfo'+'<%=model.getClientUniqueId()%>'
											},
											method:'POST',
											success : function(from, action) {
												var grid= Ext.getCmp('fileGrid'+'<%=model.getClientUniqueId()%>');
												grid.getStore().add({fileName:v});
												grid.doLayout();
												grid.getEl().unmask();
											},
											failure : function(form, action) {
												Ext.getCmp('fileGrid'+'<%=model.getClientUniqueId()%>').getEl().unmask();
												Ext.MessageBox.alert('Warning', 'File upload failed, please try again.');
											}
										});
									}
									
								}
							}
						}]	
					},{
						xtype:'grid',
						id:'fileGrid'+'<%=model.getClientUniqueId()%>',
						width:400,
						disabled:<%=order.getStatus()==3%>,
						store:{
							autoLoad:true,
							xtype:'store',
							fields:['fileName'],
							proxy : new Ext.data.HttpProxy( {
								type: 'ajax',
								url : 'getFiles.action',
								extraParams:{
									orderNumber:'<%=order.getOrderNumber()%>',
									type:'matrixInfo'+'<%=model.getClientUniqueId()%>'
								},
								reader: {
									type: 'json',
									totalProperty : 'size',
									root : 'items'
								}
							})
						},
						columns: [{
							dataIndex: 'fileName',
							width:330,
							renderer:function(value){
								return '<a href="download.action?type=matrixInfo'+'<%=model.getClientUniqueId()%>'+'&orderNumber='+'<%=order.getOrderNumber()%>'+'&fileName='+encodeURIComponent(value)+'">'+value+'</a>'
							}
						},{
							xtype:'actioncolumn',
							width:50,
							items: [{
								icon: 'images/delete.gif', 
								tooltip: 'Delete',
								handler: function(grid, rowIndex, colIndex) {
									Ext.MessageBox.confirm('Confirm', 'Are you sure you want to remove the file?', function(btn){
										if(btn=='yes'){
											Ext.getCmp('fileGrid'+'<%=model.getClientUniqueId()%>').getEl().mask('removing file...');
											Ext.Ajax.request({
												url: 'removeFile.action',
												params: {
													orderNumber:'<%=order.getOrderNumber()%>',
													type:'matrixInfo'+'<%=model.getClientUniqueId()%>',
													fileName:grid.getStore().getAt(rowIndex).data.fileName
												},
												success: function(response){
													grid.getStore().removeAt(rowIndex);
													Ext.getCmp('fileGrid'+'<%=model.getClientUniqueId()%>').getEl().unmask();
												},
												failure: function(response){
													Ext.getCmp('fileGrid'+'<%=model.getClientUniqueId()%>').getEl().unmask();
													Ext.MessageBox.alert('Warning', 'File remove failed, please try again.');
												}
											});
										}
									})
								}
							}]
						}],
						autoScroll:true,
						hideHeaders:true,
						border:false
					},{
						xtype:'label',
						html:'<hr>',
						colspan:3
					},{
						xtype:'displayfield',
						width:120,
						value:'BOM Information:'
					},{
						xtype:'form',
						fileUpload: true,
						id:'fileForm2'+'<%=model.getClientUniqueId()%>',
						border:false,
						width:250,
						items:[{
							xtype: 'fileuploadfield',
							name: 'uploadFile',
							id:'uploadFile2'+'<%=model.getClientUniqueId()%>',
							disabled:<%=order.getStatus()==3%>,
							buttonOnly: true,
							width:250,
							buttonText: 'Select File...',
							listeners: {
								'change': function(fb, v){
									if(v!=''&&v!=null){
										if(v.indexOf('\\')>-1){
											v = v.substr(v.lastIndexOf('\\')+1);
										}
										Ext.getCmp('fileGrid2'+'<%=model.getClientUniqueId()%>').getEl().mask('uploading file...');
										Ext.getCmp('fileForm2'+'<%=model.getClientUniqueId()%>').getForm().submit({
											url:'uploadFile.action',
											params:{
												orderNumber:'<%=order.getOrderNumber()%>',
												fileName: v,
												type:'matrixInfo2'+'<%=model.getClientUniqueId()%>'
											},
											method:'POST',
											success : function(from, action) {
												var grid=Ext.getCmp('fileGrid2'+'<%=model.getClientUniqueId()%>');
												grid.getStore().add({fileName:v});
												grid.doLayout();
												grid.getEl().unmask();
											},
											failure : function(form, action) {
												Ext.getCmp('fileGrid2'+'<%=model.getClientUniqueId()%>').getEl().unmask();
												Ext.MessageBox.alert('Warning', 'File upload failed, please try again.');
											}
										});
									}
									
								}
							}
						}]	
					},{
						xtype:'grid',
						id:'fileGrid2'+'<%=model.getClientUniqueId()%>',
						width:400,
						disabled:<%=order.getStatus()==3%>,
						store:{
							autoLoad:true,
							xtype:'store',
							fields:['fileName'],
							proxy : new Ext.data.HttpProxy( {
								type: 'ajax',
								url : 'getFiles.action',
								extraParams:{
									orderNumber:'<%=order.getOrderNumber()%>',
									type:'matrixInfo2'+'<%=model.getClientUniqueId()%>'
								},
								reader: {
									type: 'json',
									totalProperty : 'size',
									root : 'items'
								}
							})
						},
						columns: [{
							dataIndex: 'fileName',
							width:330,
							renderer:function(value){
								return '<a href="download.action?type=matrixInfo2'+'<%=model.getClientUniqueId()%>'+'&orderNumber='+'<%=order.getOrderNumber()%>'+'&fileName='+encodeURIComponent(value)+'">'+value+'</a>'
							}
						},{
							xtype:'actioncolumn',
							width:50,
							items: [{
								icon: 'images/delete.gif', 
								tooltip: 'Delete',
								handler: function(grid, rowIndex, colIndex) {
									Ext.MessageBox.confirm('Confirm', 'Are you sure you want to remove the file?', function(btn){
										if(btn=='yes'){
											Ext.getCmp('fileGrid2'+'<%=model.getClientUniqueId()%>').getEl().mask('removing file...');
											Ext.Ajax.request({
												url: 'removeFile.action',
												params: {
													orderNumber:'<%=order.getOrderNumber()%>',
													type:'matrixInfo2'+'<%=model.getClientUniqueId()%>',
													fileName:grid.getStore().getAt(rowIndex).data.fileName
												},
												success: function(response){
													grid.getStore().removeAt(rowIndex);
													Ext.getCmp('fileGrid2'+'<%=model.getClientUniqueId()%>').getEl().unmask();
												},
												failure: function(response){
													Ext.getCmp('fileGrid2'+'<%=model.getClientUniqueId()%>').getEl().unmask();
													Ext.MessageBox.alert('Warning', 'File remove failed, please try again.');
												}
											});
										}
									})
								}
							}]
						}],
						autoScroll:true,
						hideHeaders:true,
						border:false
					}]
				}
			<%}%>
			],	
			buttonAlign:'center',
			buttons:[{
				text:'Back',
				cls:'big-button',
				handler:goBack
			},{
				text:'Send Mail',
				cls:'big-button',
				handler:sendMail
			}]
		});
	}
});
</script>

<%@ include file="footer.jsp"%>